<?php
namespace Cli\Controller;

use Cli\Controller\CommonController;
use Library\Wx;

class UpdatedeviceController extends CommonController {
    function update(){
    	set_time_limit(0);
		$users = $this->update_users();
		$device = $this->update_device();
		$store = $this->update_store();
		$relation = $this->update_relation();
        \Think\Log::record('update ~users:'.$users.'~device:'.$device.'~store:'.$store.'~relation:'.$relation);
	}

	public function update_users(){
		$this->susers_model = D("Susers");
   		$sql = "SELECT * FROM `user` WHERE (TO_DAYS( NOW() ) - TO_DAYS(`add_time`) <= 1 or TO_DAYS( NOW() ) - TO_DAYS(`update_time`) <= 1 )";
		$this->baobaotang_model = M(NULL,NULL,C('BBT_DB'));
		$susers =$this->baobaotang_model->query($sql);
		if($susers){
			foreach($susers as $suser){
					$users['oid'] = $suser["id"];
					$users['realname'] = $suser["realname"];
					$users['mobile'] = $suser["mobile"];
					$users['sex'] = $suser["gender"];
					$users['password'] = $suser["password"];
					$user = $this->susers_model->where(array('oid'=>$suser["id"]))->find();
				if($user){
					$this->susers_model->where(array('oid'=>$suser["id"]))->save($users);
				}else{
					$this->susers_model->add($users);
				}
			}
			return 'success-数据更新成功';
		}else{
			return 'error-未连接到数据库或者未查询到需要更新的数据';
		}
	}
	public function update_device(){
		$this->device_model = D("Device");
		$sql = "SELECT * FROM device WHERE (TO_DAYS( NOW() ) - TO_DAYS(`add_time`) <= 1 )";
		$this->baobaotang_model = M(NULL,NULL,C('BBT_DB'));
		$devices =$this->baobaotang_model->query($sql);
		if($devices){
			$machine = $this->machine_expl();
			foreach($devices as $device){
				foreach($machine as $expl){
					if($expl['type']==substr($device['code'],0,2)){
						$type = $expl['type'];
						$remark = $this->remark($expl['type'],substr($device['code'],2,4));
					}
				}
				$date['barcode'] = $device['code'];
				$date['livetime'] = $device['add_time'];
				$date['dtype'] =$type;
				$date['status'] =$device['status'];
				$date['remark'] = $remark;
				if(!$this->device_model->where(array('barcode'=>$device["code"]))->find()){
					$this->device_model->add($date);
				}
			}
			return 'success-数据更新成功';
		}else{
			return 'error-未连接到数据库或者未查询到需要更新的数据';
		}
	}
	public function update_store(){
		$this->store_model = D("Store");
		$sql = "SELECT * FROM store WHERE (TO_DAYS( NOW() ) - TO_DAYS(`add_time`) <= 1 or TO_DAYS( NOW() ) - TO_DAYS(`update_time`) <= 1)";
		$this->baobaotang_model = M(NULL,NULL,C('BBT_DB'));
		$stores =$this->baobaotang_model->query($sql);
		if($stores){
			foreach($stores as $store){
				$date['store_id'] = $store['id'];
				$date['title'] = $store['title'];
				$date['title_branch'] = $store['title_branch'];
				$date['address'] = $store['address'];
				$date['cover'] = $store['cover'];
				$date['stype'] = $store['type'];
				$date['status'] = $store['status'];
				$date['province_id'] = $store['province_id'];
				$date['city_id'] = $store['city_id'];
				$date['district_id'] = $store['district_id'];
				$date['longitude'] = $store['longitude'];
				$date['latitude'] = $store['latitude'];
				$date['add_time'] = $store['add_time'];
				$date['update_time'] = $store['update_time'];
				$date['zzb_id'] =$store['zzb_id'];
				$date['user_id'] = $store['user_id'];
				$date['owner_id'] = $store['owner_id'];
				$date['agency_id'] = $store['agency_id'];
				$date['marketing_id'] = $store['marketing_id'];
				$date['sign_user_id'] = $store['sign_user_id'];
				$beaco = $this->store_model->where(array('store_id'=>$store["id"]))->find();
				if($beaco){
					$this->store_model->where(array('store_id'=>$store["id"]))->save($date);
				}else{
					$this->store_model->add($date);
				}
			}
			return 'success-数据更新成功';
		}else{
			return 'error-未连接到数据库或者未查询到需要更新的数据';
		}
	}
	public function update_relation(){
		$this->device_model = D("Device");
		$sql = "SELECT d.`code`,sdr.`store_id`,sdr.`status`,sdr.`add_time` FROM store_device_relation AS sdr,device AS d
					WHERE d.id=sdr.dev_id AND (TO_DAYS( NOW() ) - TO_DAYS(sdr.`add_time`) <= 1)";
		$this->baobaotang_model = M(NULL,NULL,C('BBT_DB'));
		$relations =$this->baobaotang_model->query($sql);
		if($relations){
			foreach($relations as $relation){
				$date['barcode'] = $relation['code'];
				$date['store_id'] = $relation['store_id'];
				$date['status'] = $relation['status'];
				$date['updatetime'] = $relation['add_time'];
				$device = $this->device_model->where(array('barcode'=>$relation['code']))->find();
				if($device){
					if (!empty($device['devid'])) {
						if($relation['status'] == 2){
							$page_ids = array((int)C('ACTIVITY_PAGEID'));
							$result = Wx::bindPageShakeAroundDevice((int)$device['devid'], $page_ids);
						}else{
							$page_ids = array((int)C('PREVIEW_PAGEID'));
							$result = Wx::bindPageShakeAroundDevice((int)$device['devid'], $page_ids);
						}
					}
					$this->device_model->where(array('barcode'=>$relation["code"]))->save($date);
				}
			}
			return 'success-数据更新成功';
		}else{
			return 'error-未连接到数据库或者未查询到需要更新的数据';
		}
	}

	public function machine_expl(){
		$expl = array(
			array('id'=>5,'type'=>'ZX','name'=>'II代保温箱'),
			array('id'=>1,'type'=>'MX','name'=>'III代保温箱'),
			array('id'=>2,'type'=>'AD','name'=>'广告机'),
			array('id'=>3,'type'=>'TA','name'=>'游戏娱乐桌'),
			array('id'=>4,'type'=>'CM','name'=>'自动咖啡机'),
			array('id'=>6,'type'=>'QT','name'=>'其他设备'),
			array('id'=>7,'type'=>'FX','name'=>'IV代保温箱'),
			array('id'=>8,'type'=>'OS','name'=>'外置摇一摇设备'),
			array('id'=>9,'type'=>'CS','name'=>'影院摇一摇设备'),
		);
		return $expl;
	}
	public function remark($type,$length){
		$remarks = array(
			array(//'type'=>'ZX',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'MX',
				array(
					array('size'=>'1','name'=>'无屏工程机'),
					array('size'=>'2','name'=>'无屏样机'),
					array('size'=>'3','name'=>'无屏量产机'),
					array('size'=>'4','name'=>'无屏次品'),
					array('size'=>'5','name'=>'无屏翻新机'),
					array('size'=>'6','name'=>'有屏工程机'),
					array('size'=>'7','name'=>'有屏样机'),
					array('size'=>'8','name'=>'有屏量产机'),
					array('size'=>'9','name'=>'有屏次品'),
					array('size'=>'0','name'=>'有屏翻新机'),
				),
				array(
					array('size'=>'1','name'=>'有Ibeacon紫'),
					array('size'=>'2','name'=>'有Ibeacon白'),
					array('size'=>'3','name'=>'以后再定'),
					array('size'=>'4','name'=>'以后再定'),
					array('size'=>'5','name'=>'以后再定'),
					array('size'=>'6','name'=>'无Ibeacon紫'),
					array('size'=>'7','name'=>'无Ibeacon白'),
				)
			),
			array(//'type'=>'AD',
				array(
					array('size'=>'430','name'=>'43吋'),
					array('size'=>'180','name'=>'18吋'),
				),
				array(
					array('size'=>'1','name'=>'工程机'),
					array('size'=>'2','name'=>'样机'),
					array('size'=>'3','name'=>'量产机'),
					array('size'=>'4','name'=>'次品'),
					array('size'=>'5','name'=>'翻新机'),
				)
			),
			array(//'type'=>'CM',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'QT',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'FX',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'TA',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'OS',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
			array(//'type'=>'CS',
				array(
					array('size'=>'0','name'=>'未定义'),
				),
				array(
					array('size'=>'0','name'=>'未定义'),
				)
			),
		);
		if($type == 'ZX'){
			$remar ='II代保温箱未定义';
		}
		if($type == 'MX'){
			$type1 =substr($length,0,1);
			$type2 =substr($length,1,1);
			foreach($remarks[1][0] as$remark){
				if($remark['size']==$type1){
					$remar =$remark['name'];
				}
			}
			foreach($remarks[1][1] as$remark){
				if($remark['size']==$type2){
					$remar = $remar.'-'.$remark['name'];
				}
			}
		}
		if($type == 'AD'){
			$type1 =substr($length,0,3);
			$type2 =substr($length,3,1);
			foreach($remarks[3][0] as$remark){
				if($remark['size']==$type1){
					$remar =$remark['name'];
				}
			}
			foreach($remarks[3][1] as$remark){
				if($remark['size']==$type2){
					$remar = $remar.'-'.$remark['name'];
				}
			}
		}
		if($type == 'CM'){
			$remar ='自动咖啡机未定义';
		}
		if($type == 'QT'){
			$remar ='其他未知设备';
		}
		if($type == 'FX'){
			$remar ='四代保温箱';
		}
		if($type == 'TA'){
			$remar ='游戏娱乐桌';
		}
		if($type == 'OS'){
			$remar ='外置摇一摇设备';
		}
		if($type == 'CS'){
			$remar ='影院摇一摇设备';
		}
		return $remar;
	}
}